package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"yk/api/request"
	"yk/internal/dao"
	"yk/internal/model"
)

func AddSubject(c *gin.Context) {
	var req request.AddSubject
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "参数验证",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "增加成功",
		"data": dao.Create(&model.Subject{
			VideoId:      req.VideoId,
			Title:        req.Title,
			Introduction: req.Introduction,
			Course:       req.Course,
		}),
	})

}

func DetailSubject(c *gin.Context) {
	atoi, _ := strconv.Atoi(c.Query("id"))
	id := uint(atoi)

	var sub model.Subject
	dao.GetOneById(id, &sub)

	if id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "未查询到数据",
			"data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": sub,
	})
}

func ListSubject(c *gin.Context) {
	var list []model.Subject

	dao.ListSubject(&list)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
}
